package com.yueshop.seckill.interceptor;

import com.yueshop.common.constant.AuthConstant;
import com.yueshop.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoingUserInterceptor implements HandlerInterceptor {
    public  static  ThreadLocal<MemberRespVo> loingUser= new ThreadLocal<>();
    //执行目标方法之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //// 远程根据订单号查询订单信息直接放行，不用登录拦截
        String uri = request.getRequestURI();
        AntPathMatcher match = new AntPathMatcher();

        boolean match1 = match.match("/seckill", uri);
        if (match1){
            MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthConstant.LOGIN_USER);

            if(attribute!=null){
                //用户已经登录 ThreadLocal共享用户信息
                loingUser.set(attribute);
                return true;//放行
            }else {
                //没有登录 就去登录
                request.getSession().setAttribute("msg","请先进行登录");
                response.sendRedirect("http://auth.yueshop.com/login.html");
                return false;
            }
        }
        return true;
    }
}
